﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineTestDBHELPER;
using OnlineTestModel;

//Please add references
namespace OnlineTestDAL
{
	/// <summary>
	/// 数据访问类:questions
	/// </summary>
	public partial class QuestionsDAL
	{

		public QuestionsDAL()
		{ }
		#region  QuestionsMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return DbHelperSQL.GetMaxID("id", "questions");
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from questions");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Questions model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into questions(");
			strSql.Append("questionname,versionnumber,predifficult,chapter,part,questiontypeid,blankline,correctcount,totalcount,correctanswer,parentid,truedifficult,partitiondegree,courseid,status)");
			strSql.Append(" values (");
			strSql.Append("@questionname,@versionnumber,@predifficult,@chapter,@part,@questiontypeid,@blankline,@correctcount,@totalcount,@correctanswer,@parentid,@truedifficult,@partitiondegree,@courseid,@status)");

			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@questionname", model.QuestionName),
					new SqlParameter("@versionnumber", model.VersionNumber),
					new SqlParameter("@predifficult", model.PreDifficult),
					
					new SqlParameter("@chapter", model.Chapter),
					new SqlParameter("@part", model.Part),
					new SqlParameter("@questiontypeid", model.QuestionTypeId),
					new SqlParameter("@blankline", model.BlankLine),
					new SqlParameter("@correctanswer", model.CorrectAnswer),
					
					new SqlParameter("@parentid", model.ParentId),
                    new SqlParameter("@correctcount", model.CorrectCount),
                    new SqlParameter("@totalcount", model.TotalCount),
					new SqlParameter("@truedifficult", model.TrueDifficult),
					new SqlParameter("@partitiondegree", model.PartitionDegree),
					new SqlParameter("@courseid", model.CourseId),
					new SqlParameter("@status",model.Status)};



			object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Questions model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update questions set ");
			strSql.Append("questionname=@questionname,");
			strSql.Append("versionnumber=@versionnumber,");
			strSql.Append("predifficult=@predifficult,");

			strSql.Append("chapter=@chapter,");
			strSql.Append("part=@part,");
			strSql.Append("questiontypeid=@questiontypeid,");
			strSql.Append("blankline=@blankline,");
			strSql.Append("correctanswer=@correctanswer,");

			strSql.Append("parentid=@parentid,");
            strSql.Append("correctcount=@correctcount,");
            strSql.Append("totalcount=@totalcount,");
			strSql.Append("truedifficult=@truedifficult,");
			strSql.Append("partitiondegree=@partitiondegree,");
			strSql.Append("courseid=@courseid,");
			strSql.Append("status=@status");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@questionname", model.QuestionName),
					new SqlParameter("@versionnumber", model.VersionNumber),
					new SqlParameter("@predifficult", model.PreDifficult),
					new SqlParameter("@chapter", model.Chapter),
					new SqlParameter("@part", model.Part),
					new SqlParameter("@questiontypeid", model.QuestionTypeId),
					new SqlParameter("@blankline", model.BlankLine),
					new SqlParameter("@correctanswer", model.CorrectAnswer),
					new SqlParameter("@parentid", model.ParentId),
                    new SqlParameter("@correctcount", model.CorrectCount),
                    new SqlParameter("@totalcount", model.TotalCount),
					new SqlParameter("@truedifficult", model.TrueDifficult),
					new SqlParameter("@partitiondegree", model.PartitionDegree),
					new SqlParameter("@courseid", model.CourseId),
					new SqlParameter("@status", model.Status),
					new SqlParameter("@id", model.Id)};


			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一个题目的状态
		/// </summary>
		public bool UpdateStatus(int status, int questionId)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update questions set ");
			strSql.Append("status=@status");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					
					new SqlParameter("@status",status),
					new SqlParameter("@id", questionId)};


			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from questions ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from questions ");
			strSql.Append(" where id in (" + idlist + ")  ");
			int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Questions GetModel(int id)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("select  top 1 id,questionname,versionnumber,predifficult,chapter,part,questiontypeid,blankline,correctcount,totalcount,correctanswer,parentid,truedifficult,partitiondegree,courseid,status from questions ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			Questions model = new Questions();
			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
				{
					model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if (ds.Tables[0].Rows[0]["questionname"] != null && ds.Tables[0].Rows[0]["questionname"].ToString() != "")
				{
					model.QuestionName = ds.Tables[0].Rows[0]["questionname"].ToString();
				}
				if (ds.Tables[0].Rows[0]["versionnumber"] != null && ds.Tables[0].Rows[0]["versionnumber"].ToString() != "")
				{
					model.VersionNumber = int.Parse(ds.Tables[0].Rows[0]["versionnumber"].ToString());
				}
				if (ds.Tables[0].Rows[0]["predifficult"] != null && ds.Tables[0].Rows[0]["predifficult"].ToString() != "")
				{
					model.PreDifficult = ds.Tables[0].Rows[0]["predifficult"].ToString();
				}

				if (ds.Tables[0].Rows[0]["chapter"] != null && ds.Tables[0].Rows[0]["chapter"].ToString() != "")
				{
					model.Chapter = ds.Tables[0].Rows[0]["chapter"].ToString();
				}
				if (ds.Tables[0].Rows[0]["part"] != null && ds.Tables[0].Rows[0]["part"].ToString() != "")
				{
					model.Part = ds.Tables[0].Rows[0]["part"].ToString();
				}
				if (ds.Tables[0].Rows[0]["questiontypeid"] != null && ds.Tables[0].Rows[0]["questiontypeid"].ToString() != "")
				{
					model.QuestionTypeId = int.Parse(ds.Tables[0].Rows[0]["questiontypeid"].ToString());
				}
				if (ds.Tables[0].Rows[0]["blankline"] != null && ds.Tables[0].Rows[0]["blankline"].ToString() != "")
				{
					model.BlankLine = int.Parse(ds.Tables[0].Rows[0]["blankline"].ToString());
				}
				if (ds.Tables[0].Rows[0]["correctanswer"] != null && ds.Tables[0].Rows[0]["correctanswer"].ToString() != "")
				{
					model.CorrectAnswer = ds.Tables[0].Rows[0]["correctanswer"].ToString();
				}
                if (ds.Tables[0].Rows[0]["correctcount"] != null && ds.Tables[0].Rows[0]["correctcount"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["correctcount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["totalcount"] != null && ds.Tables[0].Rows[0]["totalcount"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["totalcount"].ToString());
                }
				if (ds.Tables[0].Rows[0]["parentid"] != null && ds.Tables[0].Rows[0]["parentid"].ToString() != "")
				{
					model.ParentId = int.Parse(ds.Tables[0].Rows[0]["parentid"].ToString());
				}
				if (ds.Tables[0].Rows[0]["truedifficult"] != null && ds.Tables[0].Rows[0]["truedifficult"].ToString() != "")
				{
					model.TrueDifficult = ds.Tables[0].Rows[0]["truedifficult"].ToString();
				}
				if (ds.Tables[0].Rows[0]["partitiondegree"] != null && ds.Tables[0].Rows[0]["partitiondegree"].ToString() != "")
				{
					model.PartitionDegree = ds.Tables[0].Rows[0]["partitiondegree"].ToString();
				}
				if (ds.Tables[0].Rows[0]["courseid"] != null && ds.Tables[0].Rows[0]["courseid"].ToString() != "")
				{
					model.CourseId = int.Parse(ds.Tables[0].Rows[0]["courseid"].ToString());
				}
				if (ds.Tables[0].Rows[0]["status"] != null && ds.Tables[0].Rows[0]["status"].ToString() != "")
				{
					model.Status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select id,questionname,versionnumber,predifficult,difficultcount,chapter,part,questiontypeid,blankline,correctanswer,score,parentid,truedifficult,partitiondegree,courseid ");
			strSql.Append(" FROM questions ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select ");
			if (Top > 0)
			{
				strSql.Append(" top " + Top.ToString());
			}
			strSql.Append(" id,questionname,versionnumber,predifficult,difficultcount,chapter,part,questiontypeid,blankline,correctanswer,score,parentid,truedifficult,partitiondegree,courseid ");
			strSql.Append(" FROM questions ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数(questiontype=?)
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) FROM questions ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby);
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from questions T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "questions";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
		#region questionEx
		/// <summary>
		/// 得到QuestionEx实体列表
		/// </summary>
		public List<QuestionEx> GetQuestionExList(int courseid)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("select id,chapter,part,questiontypeid,truedifficult,courseid,status from questions ");
			strSql.Append(" where status=1 and courseid=@courseid");
			SqlParameter[] parameters = {
					new SqlParameter("@courseid",courseid )
			};

			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				List<QuestionEx> questionList = new List<QuestionEx>();
				for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
				{
					QuestionEx model = new QuestionEx();
					if (ds.Tables[0].Rows[i]["id"] != null && ds.Tables[0].Rows[i]["id"].ToString() != "")
					{
						model.Id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
					}
					if (ds.Tables[0].Rows[i]["chapter"] != null && ds.Tables[0].Rows[i]["chapter"].ToString() != "")
					{
						if (ds.Tables[0].Rows[i]["part"] != null && ds.Tables[0].Rows[i]["part"].ToString() != "")
						{
							model.Points = Convert.ToInt32(ds.Tables[0].Rows[i]["chapter"]) * 100 + Convert.ToInt32(ds.Tables[0].Rows[i]["part"].ToString());
						}
					}

					if (ds.Tables[0].Rows[i]["questiontypeid"] != null && ds.Tables[0].Rows[i]["questiontypeid"].ToString() != "")
					{
						model.Type = int.Parse(ds.Tables[0].Rows[i]["questiontypeid"].ToString());
					}
					if (ds.Tables[0].Rows[i]["truedifficult"] != null && ds.Tables[0].Rows[i]["truedifficult"].ToString() != "")
					{
						model.Difficulty = Convert.ToDouble(ds.Tables[0].Rows[i]["truedifficult"].ToString());
					}

					questionList.Add(model);
				}
				return questionList;
			}
			else
			{
				return null;
			}
		}
		#endregion
		#region paper和 paperquestions 连接查询
		/// <summary>
		/// 获得某试卷的数据列表
		/// </summary>
		public List<PaperQuestionEx> GetListByPaperId(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append(
				"SELECT   paperquestions.id, paperquestions.paperid, paperquestions.questionid, paperquestions.bigquestion,");
			strSql.Append(" paperquestions.smallquestion, paperquestions.score, questions.questionname, questions.versionnumber,");
			strSql.Append(" questions.predifficult, questions.chapter, questions.part, questions.questiontypeid, questions.blankline, ");
            strSql.Append("questions.correctanswer,questions.correctcount,questions.totalcount, questions.parentid, questions.truedifficult, questions.partitiondegree, questions.courseid, ");
			strSql.Append("questions.status FROM      paperquestions INNER JOIN questions ON paperquestions.questionid = questions.id");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" WHERE paperquestions.paperid " + strWhere);
			}
		    List<PaperQuestionEx> modelList = new List<PaperQuestionEx>();
            DataTable dt =DbHelperSQL.Query(strSql.ToString()).Tables[0];
            int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				PaperQuestionEx model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new PaperQuestionEx();
					if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
					{
						model.Id = int.Parse(dt.Rows[n]["id"].ToString());
					}
                    if(dt.Rows[n]["paperid"]!=null && dt.Rows[n]["paperid"].ToString()!="")
					{
						model.PaperId=int.Parse(dt.Rows[n]["paperid"].ToString());
					}
					if(dt.Rows[n]["questionid"]!=null && dt.Rows[n]["questionid"].ToString()!="")
					{
						model.QuestionId=int.Parse(dt.Rows[n]["questionid"].ToString());
					}
					if(dt.Rows[n]["bigquestion"]!=null && dt.Rows[n]["bigquestion"].ToString()!="")
					{
						model.BigQuestion=int.Parse(dt.Rows[n]["bigquestion"].ToString());
					}
					if(dt.Rows[n]["smallquestion"]!=null && dt.Rows[n]["smallquestion"].ToString()!="")
					{
						model.SmallQuestion=int.Parse(dt.Rows[n]["smallquestion"].ToString());
					}
					if(dt.Rows[n]["score"]!=null && dt.Rows[n]["score"].ToString()!="")
					{
						model.Score=int.Parse(dt.Rows[n]["score"].ToString());
					}
					if (dt.Rows[n]["questionname"] != null && dt.Rows[n]["questionname"].ToString() != "")
					{
						model.QuestionName = dt.Rows[n]["questionname"].ToString();
					}
					if (dt.Rows[n]["versionnumber"] != null && dt.Rows[n]["versionnumber"].ToString() != "")
					{
						model.VersionNumber = int.Parse(dt.Rows[n]["versionnumber"].ToString());
					}
					if (dt.Rows[n]["predifficult"] != null && dt.Rows[n]["predifficult"].ToString() != "")
					{
						model.PreDifficult = dt.Rows[n]["predifficult"].ToString();
					}

					if (dt.Rows[n]["chapter"] != null && dt.Rows[n]["chapter"].ToString() != "")
					{
						model.Chapter = dt.Rows[n]["chapter"].ToString();
					}
					if (dt.Rows[n]["part"] != null && dt.Rows[n]["part"].ToString() != "")
					{
						model.Part = dt.Rows[n]["part"].ToString();
					}
					if (dt.Rows[n]["questiontypeid"] != null && dt.Rows[n]["questiontypeid"].ToString() != "")
					{
						model.QuestionTypeId = int.Parse(dt.Rows[n]["questiontypeid"].ToString());
					}
					if (dt.Rows[n]["blankline"] != null && dt.Rows[n]["blankline"].ToString() != "")
					{
						model.BlankLine = int.Parse(dt.Rows[n]["blankline"].ToString());
					}
					if (dt.Rows[n]["correctanswer"] != null && dt.Rows[n]["correctanswer"].ToString() != "")
					{
						model.CorrectAnswer = dt.Rows[n]["correctanswer"].ToString();
					}
                    if (dt.Rows[n]["correctcount"] != null && dt.Rows[n]["correctcount"].ToString() != "")
                    {
                        model.ParentId = int.Parse(dt.Rows[n]["correctcount"].ToString());
                    }
                    if (dt.Rows[n]["totalcount"] != null && dt.Rows[n]["totalcount"].ToString() != "")
                    {
                        model.ParentId = int.Parse(dt.Rows[n]["totalcount"].ToString());
                    }
					if (dt.Rows[n]["parentid"] != null && dt.Rows[n]["parentid"].ToString() != "")
					{
						model.ParentId = int.Parse(dt.Rows[n]["parentid"].ToString());
					}
					if (dt.Rows[n]["truedifficult"] != null && dt.Rows[n]["truedifficult"].ToString() != "")
					{
						model.TrueDifficult = dt.Rows[n]["truedifficult"].ToString();
					}
					if (dt.Rows[n]["partitiondegree"] != null && dt.Rows[n]["partitiondegree"].ToString() != "")
					{
						model.PartitionDegree = dt.Rows[n]["partitiondegree"].ToString();
					}
					if (dt.Rows[n]["courseid"] != null && dt.Rows[n]["courseid"].ToString() != "")
					{
						model.CourseId = int.Parse(dt.Rows[n]["courseid"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
			
		}
		#endregion


	}
}

